<?php
namespace app\admin\controller;
use app\admin\controller\Base;
use think\Db;
use think\Controller;
use app\common\model\Category as CateModel;

class Category extends Base
{
    protected $beforeActionList = [

        'delsoncate'  =>  ['only'=>'del'],
    ];

    public function index()
    {
        $cate=new CateModel;
        if(request()->isPost()){
            $sorts=input('post.');
            foreach ($sorts as $k => $v) {
                $cate->update(['cate_id'=>$k,'displayorder'=>$v]);
            }
            $this->success('更新排序成功！',url('index'));
            return;
        }
        $category=model('Category')->catetree();

        return $this->fetch('',['category'=>$category]);


    }
        public function add()
        {
            $category=model('Category')->catetree();
            return $this->fetch('',['category'=>$category]);
        }

        public function save()
        {
            if(request()->isPost()) {

                $data = input('post.');
                $cate_id=model('category')->create($data, true);;
                if($cate_id)
                {
                    return $this->success('新增成功','index');
                }else{
                    return $this->success('新增失败','index');
                }



            }
        }
        public function del($id)
        {
            $goods =model('category')::find($id);
            $goods->delete();


            $this->success('删除成功', 'index');
        }
    public function delsoncate(){
       $id=input('id');
        $cate=new CateModel();
        $sonids=$cate->getchilrenid($id);
        if($sonids){
           db('Category')->delete($sonids);
        }
    }


    public function edit($id)
    {
        $cate =model('Category')::find($id);
        $catelist=model('Category')->catetree();


        return $this->fetch('edit',['cate'=>$cate,
            'catelist'=>$catelist
            ]);
    }
    public function update()
    {
        $data = input('post.');

            model('Category')::update($data,['cate_id' => $data['cate_id']], true);


        $this->success('修改成功', 'index');
    }
    
   
}